package com.zhicheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhicheng.mapper.CategoryMapper;
import com.zhicheng.mapper.GoodMapper;
import com.zhicheng.mapper.UserMapper;
import com.zhicheng.pojo.*;
import com.zhicheng.pojo.dto.GoodDTO;
import com.zhicheng.pojo.result.PageResult;
import com.zhicheng.pojo.vo.CategoryTwoVO;
import com.zhicheng.pojo.vo.GoodVO;
import com.zhicheng.service.GoodService;
import com.zhicheng.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public GoodVO getGoodsById(Long id) {
        GoodVO goodVO = goodMapper.getGoodsById(id);
        Long sellerId = goodVO.getSellerId();
        User user = userMapper.getUserById(sellerId);
        goodVO.setSellerName(user.getNickname());
        List<String> pictures = goodMapper.getPicturesByGoodId(id);
        goodVO.setPictures(pictures);
        return goodVO;
    }

    @Override
    public List<GoodVO> getHotGoodsByLikeCount(Long id,Long limit) {
        GoodVO good = goodMapper.getGoodsById(id);
        Long parentId = good.getParentId();
        List<GoodVO> goods = goodMapper.getHotGoodsByLikeCount(parentId,limit);

        return goods;
    }

    @Override
    public PageResult<Good> list(int page, int pageSize,Integer categoryId, Integer status) {
        //1.创建PageResult对象
        PageResult<Good> pageResult =new PageResult<>();
        //2.开启分页查询PageHelper
        PageHelper.startPage(page, pageSize);
        Long userId = ThreadLocalUtil.getCurrentId();
        List<Good> goodList = goodMapper.list(userId,categoryId,status);
        //Page中提供了方法，可以获取PageHelper分页查询后得到的总记录数和当前页数据
        Page<Good> p = (Page<Good>) goodList;
        //把数据填充到PageResult对象中
        pageResult.setTotal(p.getTotal());
        pageResult.setItems(p.getResult());
        pageResult.setPages(p.getPages());
        pageResult.setPage(p.getPageNum());
        pageResult.setPageSize(p.getPageSize());
        return pageResult;
    }

    @Override
    public void add(GoodDTO goodDTO) {
        Good good = new Good();
        BeanUtils.copyProperties(goodDTO,good);
        good.setCreateTime(LocalDateTime.now());
        good.setUpdateTime(LocalDateTime.now());
        CategoryTwoVO categoryTwo = categoryMapper.selectCategoryTwoById(goodDTO.getCategoryId());
        good.setParentId(categoryTwo.getCategoryId());
        good.setParentName(categoryTwo.getParentName());
        good.setCategoryName(categoryTwo.getName());
        good.setLikeCount(0L);
        good.setCollectCount(0L);
        good.setCommentCount(0L);
        Long userId = ThreadLocalUtil.getCurrentId();
        good.setSellerId(userId);
        goodMapper.add(good);
        Long newGoodId = good.getId();
        List<String> img_urlList = goodDTO.getPictures();
        for (String img_url : img_urlList){
            Pictures pictures = new Pictures();
            pictures.setImgUrl(img_url);
            pictures.setCreateTime(LocalDateTime.now());
            pictures.setUpdateTime(LocalDateTime.now());
            pictures.setGoodId(newGoodId);
            goodMapper.addPictures(pictures);
        }

    }

    @Override
    public void updateGoods(GoodDTO goodDTO) {
        goodDTO.setUpdateTime(LocalDateTime.now());
        goodMapper.updateGoods(goodDTO);
        Long goodId = goodDTO.getId();
        List<String> img_urlList = goodDTO.getPictures();
        for (String img_url : img_urlList){
            Pictures pictures = new Pictures();
            pictures.setImgUrl(img_url);
            pictures.setCreateTime(LocalDateTime.now());
            pictures.setUpdateTime(LocalDateTime.now());
            pictures.setGoodId(goodId);
            goodMapper.addPictures(pictures);
        }
    }

    @Override
    public void deleteGoods(Long id) {
        goodMapper.deleteGoods(id);
        goodMapper.deletePictures(id);
    }
}
